package com.tengju.data.infrastructure.repository.convert;

import com.tengju.data.domain.model.clickhouse.DwsUserDominationTeamParent;
import com.tengju.data.domain.model.clickhouse.star.*;
import com.tengju.data.domain.model.user.DominationDetail;
import com.tengju.data.domain.model.user.DominationLevelEnum;
import com.tengju.data.domain.model.user.StarLevelEnum;
import com.tengju.data.domain.model.user.UserIdCode;
import com.tengju.data.infrastructure.persistence.mybatis.dataobject.*;
import org.springframework.util.CollectionUtils;

import java.time.ZoneId;
import java.util.*;
import java.util.stream.Collectors;

public class DominationDetailConvert {

    public static DominationDetail convertToModel(DwdUserDominationDetailDO dominationDetailDO) {
        if (dominationDetailDO == null) {
            return null;
        }
        return new DominationDetail(new UserIdCode(dominationDetailDO.getIdCode()),
                StarLevelEnum.parseByStarLevel(dominationDetailDO.getStarLevel()),
                DominationLevelEnum.fromVal(dominationDetailDO.getDominationLevel()),
                Optional.ofNullable(dominationDetailDO.getParentIdCode())
                        .filter(parentIdCode -> parentIdCode > 0)
                        .map(UserIdCode::new).orElse(null),
                StarLevelEnum.parseByStarLevel(dominationDetailDO.getParentStarLevel()),
                dominationDetailDO.getCurrentMonthAchievement(),
                dominationDetailDO.getDominationEcNum(),
                dominationDetailDO.getDominationCityStarOneNum(),
                dominationDetailDO.getDominationCityStarTwoNum(),
                dominationDetailDO.getDominationCityStarThreeNum(),
                dominationDetailDO.getDominationCityStarFourNum(),
                dominationDetailDO.getDominationDealerNum(),
                dominationDetailDO.getRegisterTime() == null ? null : dominationDetailDO.getRegisterTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(),
                dominationDetailDO.getUserLevelFourCnt(),
                dominationDetailDO.getUserLevelThreeCnt(),
                dominationDetailDO.getUserLevelTwoCnt());

    }

    public static DominationMemberNew convertToMemberNewModel(List<DominationTeamCountDO> dominationTeamCountDOList) {

        Map<StarLevelEnum, Integer> resultMap = new HashMap<>();

        for (DominationTeamCountDO dominationTeamCountDO : dominationTeamCountDOList) {
            StarLevelEnum starLevel = StarLevelEnum.parseByStarLevel(dominationTeamCountDO.getStarLevel());
            if (starLevel != null) {
                resultMap.put(starLevel, dominationTeamCountDO.getNum());
            }
        }

        return new DominationMemberNew(Optional.ofNullable(resultMap.get(StarLevelEnum.EC)).orElse(0),
                Optional.ofNullable(resultMap.get(StarLevelEnum.CITY_STAR_ONE)).orElse(0),
                Optional.ofNullable(resultMap.get(StarLevelEnum.CITY_STAR_TWO)).orElse(0),
                Optional.ofNullable(resultMap.get(StarLevelEnum.CITY_STAR_THREE)).orElse(0),
                Optional.ofNullable(resultMap.get(StarLevelEnum.CITY_STAR_FOUR)).orElse(0),
                Optional.ofNullable(resultMap.get(StarLevelEnum.NONE)).orElse(0));

    }

    public static List<DominationIdCodeMemberNew> convertToIdCodeMemberNewModel(List<DominationIdCodeTeamCountDO> dominationIdCodeTeamCountDOList,
                                                                                List<DominationIdCodeTeamCountDO> dealerDominationIdCodeTeamCountDOList) {
        List<DominationIdCodeMemberNew> result = new ArrayList<>();

        Map<Long, Map<StarLevelEnum, Integer>> map = new HashMap<>();
        for (DominationIdCodeTeamCountDO dominationTeamCountDO : dominationIdCodeTeamCountDOList) {
            StarLevelEnum starLevel = StarLevelEnum.parseByStarLevel(dominationTeamCountDO.getStarLevel());
            if (starLevel != null) {
                if (map.containsKey(dominationTeamCountDO.getDominationIdCode())) {
                    Map<StarLevelEnum, Integer> starLevelCountMap = map.get(dominationTeamCountDO.getDominationIdCode());
                    starLevelCountMap.put(starLevel, dominationTeamCountDO.getNum());
                } else {
                    Map<StarLevelEnum, Integer> starLevelCountMap = new HashMap<>();
                    starLevelCountMap.put(starLevel, dominationTeamCountDO.getNum());
                    map.put(dominationTeamCountDO.getDominationIdCode(), starLevelCountMap);
                }
            }
        }

        for (DominationIdCodeTeamCountDO dominationTeamCountDO : dealerDominationIdCodeTeamCountDOList) {
            StarLevelEnum starLevel = StarLevelEnum.NONE;
            if (map.containsKey(dominationTeamCountDO.getDominationIdCode())) {
                Map<StarLevelEnum, Integer> starLevelCountMap = map.get(dominationTeamCountDO.getDominationIdCode());
                starLevelCountMap.put(starLevel, dominationTeamCountDO.getNum());
            } else {
                Map<StarLevelEnum, Integer> starLevelCountMap = new HashMap<>();
                starLevelCountMap.put(starLevel, dominationTeamCountDO.getNum());
                map.put(dominationTeamCountDO.getDominationIdCode(), starLevelCountMap);
            }
        }

        for (Map.Entry<Long, Map<StarLevelEnum, Integer>> entry : map.entrySet()) {
            Map<StarLevelEnum, Integer> resultMap = entry.getValue();
            DominationIdCodeMemberNew dominationIdCodeMemberNew = new DominationIdCodeMemberNew(
                    new UserIdCode(entry.getKey()),
                    Optional.ofNullable(resultMap.get(StarLevelEnum.EEC)).orElse(0),
                    Optional.ofNullable(resultMap.get(StarLevelEnum.SEC)).orElse(0),
                    Optional.ofNullable(resultMap.get(StarLevelEnum.EC)).orElse(0),
                    Optional.ofNullable(resultMap.get(StarLevelEnum.CITY_STAR_ONE)).orElse(0),
                    Optional.ofNullable(resultMap.get(StarLevelEnum.CITY_STAR_TWO)).orElse(0),
                    Optional.ofNullable(resultMap.get(StarLevelEnum.CITY_STAR_THREE)).orElse(0),
                    Optional.ofNullable(resultMap.get(StarLevelEnum.CITY_STAR_FOUR)).orElse(0),
                    Optional.ofNullable(resultMap.get(StarLevelEnum.NONE)).orElse(0));
            result.add(dominationIdCodeMemberNew);
        }
        return result;
    }


    public static UpgradingDetail convertToModel(UpgradingDetailDO upgradingDetailDO) {

        if (upgradingDetailDO == null) {
            return null;
        }

        return new UpgradingDetail(new UserIdCode(upgradingDetailDO.getIdCode()),
                upgradingDetailDO.getUpgradingTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(),
                StarLevelEnum.parseByStarLevel(upgradingDetailDO.getStarLevel()));

    }


    public static DominationUpComing convertToUpcomingModel(List<DominationTeamCountDO> dominationTeamCountDOList) {

        Map<StarLevelEnum, Integer> resultMap = new HashMap<>();

        for (DominationTeamCountDO dominationTeamCountDO : dominationTeamCountDOList) {
            StarLevelEnum starLevel = StarLevelEnum.parseByStarLevel(dominationTeamCountDO.getStarLevel());
            if (starLevel != null) {
                resultMap.put(starLevel, dominationTeamCountDO.getNum());
            }
        }

        return new DominationUpComing(Optional.ofNullable(resultMap.get(StarLevelEnum.EEC)).orElse(0),
                Optional.ofNullable(resultMap.get(StarLevelEnum.SEC)).orElse(0),
                Optional.ofNullable(resultMap.get(StarLevelEnum.EC)).orElse(0),
                Optional.ofNullable(resultMap.get(StarLevelEnum.CITY_STAR_ONE)).orElse(0),
                Optional.ofNullable(resultMap.get(StarLevelEnum.CITY_STAR_TWO)).orElse(0),
                Optional.ofNullable(resultMap.get(StarLevelEnum.CITY_STAR_THREE)).orElse(0),
                Optional.ofNullable(resultMap.get(StarLevelEnum.CITY_STAR_FOUR)).orElse(0),
                Optional.ofNullable(resultMap.get(StarLevelEnum.NONE)).orElse(0));

    }


    public static DominationSigningMember convertToSigningMemberModel(List<DominationTeamCountDO> dominationTeamCountDOList) {

        Map<StarLevelEnum, Integer> resultMap = new HashMap<>();

        for (DominationTeamCountDO dominationTeamCountDO : dominationTeamCountDOList) {
            StarLevelEnum starLevel = StarLevelEnum.parseByStarLevel(dominationTeamCountDO.getStarLevel());
            if (starLevel != null) {
                resultMap.put(starLevel, dominationTeamCountDO.getNum());
            }
        }

        return new DominationSigningMember(Optional.ofNullable(resultMap.get(StarLevelEnum.EEC)).orElse(0),
                Optional.ofNullable(resultMap.get(StarLevelEnum.SEC)).orElse(0),
                Optional.ofNullable(resultMap.get(StarLevelEnum.EC)).orElse(0),
                Optional.ofNullable(resultMap.get(StarLevelEnum.CITY_STAR_ONE)).orElse(0),
                Optional.ofNullable(resultMap.get(StarLevelEnum.CITY_STAR_TWO)).orElse(0),
                Optional.ofNullable(resultMap.get(StarLevelEnum.CITY_STAR_THREE)).orElse(0),
                Optional.ofNullable(resultMap.get(StarLevelEnum.CITY_STAR_FOUR)).orElse(0));

    }

    public static List<DwsUserDominationTeamParent> convert(List<DwdUserDominationTeamParentDO> dwdUserDominationTeamParentDOList) {

        if (CollectionUtils.isEmpty(dwdUserDominationTeamParentDOList)) {
            return new ArrayList<>();
        }

        return dwdUserDominationTeamParentDOList.stream()
                .map(d -> new DwsUserDominationTeamParent(
                        UserIdCode.of(d.getIdCode()),
                        StarLevelEnum.parseByStarLevel(d.getStarLevel()),
                        d.getDominationIdCode() != 0 ? UserIdCode.of(d.getDominationIdCode()) : null)
                ).collect(Collectors.toList());
    }
}
